<?php

namespace Home\Controller; 
use Common\Controller\CommonController; 

class AccessController extends CommonController {
	
	/**
	* 用户列表
	* @return
	*/
	public function manager(){
		$list = M('RbacManager')->field('a.*,b.role_id')->join(' as a left join zdk_rbac_role_manager as b on b.user_id = a.id ')->select();
		$data = array();
		foreach($list as $li){ 
			if(!empty($li['role_id'])){
				$li['role'] = M('RbacRole')->where(' id = '.$li['role_id'])->getField('name');
			}else{
				$li['role'] = '系统管理员';
			}
			$data[] = $li;
		} 
		$roledata = M('RbacRole')->where('id>1')->select(); 
		$assign = array('list'=>$data,'rolelist'=>$roledata); 
		$this->assign($assign);
		$this->display();
	}
	
	

	
	/**
	* 删除用户
	* @return
	*/
	public function manager_del(){
		$id=I('get.id');
		M('RbacRoleManager')->where(array('user_id'=>$id))->delete();
		M('RbacManager')->where(array('id'=>$id))->delete();
        $this->success('删除成功',U('/Access/manager'));
	}
	
	/**
	* 保存用户
	* @return
	*/
	public function manager_save(){
		$data=I('post.');
		$udata = array(
			'username'	=>	$data['username'],
			'password'	=>	MD5($data['password']),
		);
		$uid = M('RbacManager')->data($udata)->add();
		
		$rudata = array(
			'user_id'	=>	$uid,
			'role_id'	=>	$data['role_id'],
		);
		$rs = M('RbacRoleManager')->data($rudata)->add();
       	$this->success('添加成功',U('/Access/manager'));
	}
	
	/**
	* 节点管理
	* @return
	*/
	public function node(){
		$list = M('RbacNode')->select(); 
		$data = category($list);
		$this->assign('list',$data);
		$this->display();
	}
	
	/**
	* 保存节点 
	* @return
	*/
	public function node_save(){
		$data = I('post.');
		M('RbacNode')->data($data)->add();
        $this->success('节点添加成功',U('/Access/node'));
	}
	
	/**
	* 删除节点
	* @return
	*/
	public function node_del(){
		$id = I('get.id');
		$node = M('RbacNode')->where(array('id'=>$id))->delete();
        $this->success('节点删除成功',U('/Access/node'));
	}
	
	/**
	* 角色管理
	* @return
	*/
	public function role(){
		$list = M('RbacRole')->where('id>1')->select(); 
		$this->assign('list',$list);
		$this->display();
	}
	
	/**
	* 删除角色
	* @return
	*/
	public function role_del(){
		$id=I('get.id');
		M('RbacRole')->where(array('id'=>$id))->delete(); 
        $this->success('角色删除成功',U('/Access/role'));
	}
	
	/**
	* 保存角色
	* @return
	*/
	public function role_save(){
		$data=I('post.');
		$uid = M('RbacRole')->data($data)->add();
        $this->success('角色添加成功',U('/Access/role'));
	}
	
	/**
	* 访问控制
	* @return
	*/
	public function access(){
		$roleid = I('get.role_id');
		$list = M('RbacNode')->select(); 
		$acclist = M('RbacAccess')->where('role_id='.$roleid)->getField('node_id',true);
		$datlist = array();
		foreach($list as $dt){
			$dt['ischeck'] = in_array($dt['id'],$acclist)?' checked="checked" ':''; 
			$datlist[] = $dt;
		} 
		$data = category($datlist);
		$this->assign('list',$data);
		$this->assign('role_id',$roleid);
		$this->display(); 
	}
	
	/**
	* 保存角色对节点访问权限
	* @return
	*/
	public function access_save(){
		$data = I('post.');
		$role_id = $data['role_id']; 
		$datalist = array();
		foreach($data['f_mod'] as $mod){
			$datalist[] = array(
				'role_id'	=>	$role_id,
				'node_id'	=>	$mod,
				'level'	=>	1,
			);
		}
		foreach($data['f_con'] as $mod){
			$datalist[] = array(
				'role_id'	=>	$role_id,
				'node_id'	=>	$mod,
				'level'	=>	2,
			);
		}
		foreach($data['f_act'] as $mod){
			$datalist[] = array(
				'role_id'	=>	$role_id,
				'node_id'	=>	$mod,
				'level'	=>	3,
			);
		}
		M('RbacAccess')->where(array('role_id'=>$role_id))->delete();
		M('RbacAccess')->addAll($datalist);
		$this->success('权限设置完成',U('/Access/role'));
	}
}